package com.hellowx.qydcdev.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created on 2022/1/5.
 *
 * @author Hello WX
 */
@Slf4j
@RestControllerAdvice
public class WebExceptionHandler {

    @ExceptionHandler({CustomException.class})
    public ResponseEntity<String> customExceptionHandler(CustomException ex){
        if (ex.getCode()==CustomExceptionType.USER_INPUT_EXCEPTION.getCode()){
            ex.printStackTrace();
            return new ResponseEntity<>(ex.getMessage(),HttpStatus.BAD_REQUEST);
        }else if (ex.getCode()==CustomExceptionType.PERMISSION_EXCEPTION.getCode()){
            ex.printStackTrace();
            return new ResponseEntity<>(ex.getMessage(),HttpStatus.UNAUTHORIZED);
        }
        ex.printStackTrace();
        return new ResponseEntity<>(ex.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> exceptionHandler(Exception ex){
        ex.printStackTrace();
        return new ResponseEntity<>("系统异常,请联系管理员!!",HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
